<?php
// Set the initial include_path. You may need to change this to ensure that 
// Zend Framework is in the include_path; additionally, for performance 
// reasons, it's best to move this to your web server configuration or php.ini 
// for production.
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(dirname(__FILE__) . '/../library'),
    get_include_path(),
)));

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Define path to Beaver application directory
defined('BEAVER_APPLICATION_PATH')
    || define('BEAVER_APPLICATION_PATH', dirname(dirname(__FILE__)) . '/library/Beaver/application');

/** Zend_Config_Ini */
require_once 'Zend/Config/Ini.php';

// Merge the beaver and the user config files
$configBeaver = new Zend_Config_Ini( BEAVER_APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV, true);

if ( file_exists(APPLICATION_PATH . '/configs/application.ini'))
{
	$configApplication = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
	$configBeaver->merge($configApplication);
}

// Ext.Direct ini file (cached in prod only)

$directIniPath = $configBeaver->get('directIniPath');
if (APPLICATION_ENV=='production' && file_exists($directIniPath) ) {
    // load ini
    $configExt = new Zend_Config_Ini($directIniPath, APPLICATION_ENV);
    $configBeaver->merge($configExt);
}

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV, 
    $configBeaver
);

// Ext.Direct ini file Generator. (Uses the ZF autoloader configured by Zend_Application)
if(APPLICATION_ENV!='production' || !file_exists($directIniPath)) {
    // (re)generate ini file
    $writer = new Beaver_Config_Writer_Ini($directIniPath);
    $writer->write();
}

$application->bootstrap();
$application->run();
